package com.faf.ui;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.faf.dao.impl.LikesDaoImpl;

/**
 * Servlet implementation class LikeSetter
 */
@WebServlet("/LikeSetter")
public class LikeSetter extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LikeSetter() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		int fid = Integer.parseInt(request.getParameter("fid").toString());
		int uid = Integer.parseInt(request.getSession().getAttribute("id").toString());
		int action = Integer.parseInt(request.getParameter("action"));		
		
		LikesDaoImpl likeDao = new LikesDaoImpl();

		try {
			if (action == 0)
				likeDao.newLikeDelete(uid, fid);
			else
				likeDao.newLikeInsert(uid, fid);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		request.getRequestDispatcher("Detail?fid=" + request.getParameter("fid")).forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
